﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HARTCalibrationTool.变送器选择
{
    internal class ButtonWithIndicator : Button
    {
        private CircleIndicator indicator;
        private const int MIN_INDICATOR_SIZE = 0; // 指示灯的最小尺寸
        private bool isMouseDown;

        public ButtonWithIndicator()
        {
            // 创建并初始化圆形指示灯
            indicator = new CircleIndicator
            {
                LightColor = Color.FromArgb(100, 0, 0),
            };

            // 添加圆形指示灯到按钮控件中
            this.Controls.Add(indicator);
            this.FlatStyle = FlatStyle.Standard;
            this.BackColor = Color.LightGray;
            this.FlatAppearance.BorderSize = 0;

            // 设置按钮默认颜色
            this.Paint += ButtonWithLight_Paint;
            this.Resize += ButtonWithIndicator_Resize;
            this.MouseDown += ButtonWithLight_MouseDown;
            this.MouseUp += ButtonWithLight_MouseUp;
            this.MouseLeave += ButtonWithLight_MouseLeave;

            UpdateLightPosition();
        }
        protected override void OnLayout(LayoutEventArgs levent)
        {
            base.OnLayout(levent);
            UpdateLightPosition();
        }
        private void ButtonWithLight_Paint(object sender, PaintEventArgs e)
        {
            // 绘制按钮背景
            Color buttonColor = isMouseDown ? Color.DarkGray : this.BackColor;
            using (SolidBrush brush = new SolidBrush(buttonColor))
            {
                e.Graphics.FillRectangle(brush, this.ClientRectangle);
            }

            // 绘制按钮文本
            TextRenderer.DrawText(e.Graphics, this.Text, this.Font, GetTextRectangle(),
                this.ForeColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
        }
        private Rectangle GetTextRectangle()
        {
            var textSize = TextRenderer.MeasureText(this.Text, this.Font);
            var textRectangle = new Rectangle();

            switch (this.TextAlign)
            {
                case ContentAlignment.TopLeft:
                    textRectangle = new Rectangle(0, 0, textSize.Width, textSize.Height);
                    break;
                case ContentAlignment.TopCenter:
                    textRectangle = new Rectangle((this.Width - textSize.Width) / 2, 0, textSize.Width, textSize.Height);
                    break;
                case ContentAlignment.TopRight:
                    textRectangle = new Rectangle(this.Width - textSize.Width, 0, textSize.Width, textSize.Height);
                    break;
                case ContentAlignment.MiddleLeft:
                    textRectangle = new Rectangle(0, (this.Height - textSize.Height) / 2, textSize.Width, textSize.Height);
                    break;
                case ContentAlignment.MiddleCenter:
                    textRectangle = new Rectangle((this.Width - textSize.Width) / 2, (this.Height - textSize.Height) / 2, textSize.Width, textSize.Height);
                    break;
                case ContentAlignment.MiddleRight:
                    textRectangle = new Rectangle(this.Width - textSize.Width, (this.Height - textSize.Height) / 2, textSize.Width, textSize.Height);
                    break;
                case ContentAlignment.BottomLeft:
                    textRectangle = new Rectangle(0, this.Height - textSize.Height, textSize.Width, textSize.Height);
                    break;
                case ContentAlignment.BottomCenter:
                    textRectangle = new Rectangle((this.Width - textSize.Width) / 2, this.Height - textSize.Height, textSize.Width, textSize.Height);
                    break;
                case ContentAlignment.BottomRight:
                    textRectangle = new Rectangle(this.Width - textSize.Width, this.Height - textSize.Height, textSize.Width, textSize.Height);
                    break;
            }

            // 考虑指示灯的位置
            textRectangle.Width = this.Width - indicator.Width - 10;

            return textRectangle;
        }
        private void ButtonWithLight_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDown = true;
            indicator.BackgroundColor = Color.DarkGray;
            Invalidate();
        }
        private void ButtonWithLight_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
            indicator.BackgroundColor = this.BackColor;
            Invalidate();
        }
        private void ButtonWithLight_MouseLeave(object sender, EventArgs e)
        {
            if (isMouseDown)
            {
                isMouseDown = false;
                indicator.BackgroundColor = this.BackColor;
                Invalidate();
            }
        }
        private void UpdateLightPosition()
        {
            int indicatorSize = Math.Max(this.Height - 10, 20);

            indicator.Location = new Point(this.Width - indicator.Width - 5, (this.Height - indicator.Height) / 2);
            indicator.Size = new Size(indicatorSize, indicatorSize);
        }
        private void ButtonWithIndicator_Resize(object s, EventArgs e)
        {
            UpdateLightPosition();
        }
    }

    // 圆形指示灯类
    public class CircleIndicator : Control
    {
        private Color lightColor;
        private Color backgroundColor = Color.LightGray; // 默认按钮背景颜色

        public Color LightColor
        {
            get { return lightColor; }
            set { lightColor = value; Invalidate(); }
        }

        public Color BackgroundColor
        {
            get { return backgroundColor; }
            set { backgroundColor = value; Invalidate();}
        }

        public CircleIndicator()
        {
            this.Size = new Size(20, 20);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            // 绘制背景颜色
            using (Brush brush = new SolidBrush(backgroundColor))
            {
                e.Graphics.FillRectangle(brush, 0, 0, this.Width, this.Height);
            }
            // 绘制指示灯颜色
            using (SolidBrush brush = new SolidBrush(lightColor))
            {
                e.Graphics.FillEllipse(brush, 0, 0, this.Width - 1, this.Height - 1);
            }
            // 绘制按钮边框颜色
            using (Pen pen = new Pen(backgroundColor))
            {
                e.Graphics.DrawRectangle(pen, 0, 0, this.Width, this.Height);
            }
        }
    }
}
